<?php

namespace Admin\Controller;

class PaymentController extends AdminController {

    public function index() {
        $map = array('status' => array('egt', 0));
        $result = M('Payment')->where($map)->order('id asc')->select();
        $total = M('Payment')->where(['status'=>1])->count();
        $this->assign('total',$total);
        $this->assign('result',$result);
        $this->display();
    }

    public function edit($id = 0) {
        if (IS_POST) {
            $data = D('Payment')->create();
            if ($data) {
                $config = I('c', '');
                $data['config_param'] = $config ? json_encode($config) : '';
                $data['client_pc'] = $data['client_pc'] ? $data['client_pc'] : 0;
                $data['client_wap'] = $data['client_wap'] ? $data['client_wap'] : 0;
                $data['client_app'] = $data['client_app'] ? $data['client_app'] : 0;
                if (D('Payment')->save($data) !== false) {
                    //记录行为
                    $this->success('编辑成功', U('index'));
                } else {
                    $this->error('编辑失败');
                }
            } else {
                $this->error('编辑失败');
            }
        } else {
            /* 获取数据 */
            $data = M('Payment')->find($id);
            if (empty($data)) {
                $this->error('获取配置信息错误');
            }
            $data['configParam'] = \Common\Lib\Payments\Payment::createPaymentInstance($data['id'])->configParam();
            $data['paramData'] = json_decode($data['config_param'],true);
            $this->assign($data);
            $this->display();
            
        }
    }

}
